<?php

namespace App\Http\Controllers\Admin;

use App\Models\Open;
use App\Models\Admin;
use App\Models\Picture;
use App\Models\ReplyText;
use App\Models\WechatMenus;
use App\Models\WechatSet;
use App\Support\WechatSilentAuth;

class WechatController extends BaseController
{

    public function __construct(WechatSet $wechatSet, WechatMenus $wechatMenus,ReplyText $replyText,Open $open, Admin $admin)
    {
        parent::__construct();
        $this->wechatSet = $wechatSet;
        $this->wechatMenus = $wechatMenus;
        $this->replyText = $replyText;
        $this->open = $open;
        $this->admin = $admin;
    }

    public function getIndex()
    {
        $rows = $this->admin->getWeChatSetList();
        return view('admin.wechat.list',compact('rows'));
    }

    public function getSet()
    {
        $ext = (new Picture())->config['saveExt'];
        $info = $this->wechatSet->_getOne(['admin_id'=>is_login()]);

        // 判断图片是否存在
        $imgCount = 0;
        if($info) {
            if($info['wx_app_id']) {
                if(file_exists(public_path().'/uploads/share/'.$info['wx_app_id'].'_1'.'.'.$ext)) {
                    $imgCount = 1;
                }
            }
        }
        $qrImg = '';
        if(file_exists(public_path().'/uploads/qrImg/'.$info['wx_app_id'].'_qr'.'.'.$ext)) {
            $qrImg = '/uploads/qrImg/'.$info['wx_app_id'].'_qr'.'.'.$ext;
        }
        $ctr = 'wechat';
        return view('admin.wechat.set', compact('info','ext', 'imgCount', 'qrImg', 'ctr'));
    }

    public function postSet()
    {
        $res = $this->wechatSet->addOrUpdate();
        if (!$res) {
            return $this->error($this->wechatSet->getError() ? $this->wechatSet->getError() : '未知错误');
        }
        return $this->success('更新成功', urlAdmin('wechat', 'getSet'));
    }
    //微信一键授权
    public function getAuth()
    {
        $callBackUrl = urlAdmin('wechat','getAuth',['aid'=>is_login()]);
        $url = $this->open->getAuthUrl($callBackUrl);
        if (!$url) {
            return $this->error($this->open->getError() ? $this->open->getError() : '未知错误');
        }
        header('Location:'.$url);
    }


    public function getMenu()
    {
        $tree = $this->wechatMenus->getTree();
        return view('admin.wechat.menu', compact('tree'));
    }

    public function getAdd()
    {
        $menu = $this->wechatMenus->getMenuTree();
        $xcx = $this->wechatMenus->getXcx();
        $modules = $this->wechatMenus->getModules();
        $info['pid'] = request('pid');
        return view('admin.wechat.edit', compact('menu', 'info','xcx', 'modules'));
    }

    public function postAdd()
    {
        $res = $this->wechatMenus->addOrUpdate();
        if (!$res) {
            return $this->error($this->wechatMenus->getError() ? $this->wechatMenus->getError() : '新增失败');
        }
        return $this->success('新增成功', urlAdmin('wechat', 'getMenu'));
    }

    /* 编辑分类 */
    public function getEdit()
    {
        $menu = $this->wechatMenus->getMenuTree();
        $xcx = $this->wechatMenus->getXcx();
        $modules = $this->wechatMenus->getModules();
        /* 获取分类信息 */
        $info = $this->wechatMenus->getInfo(request('id'));
//        dd($info);
        return view('admin.wechat.edit', compact('info', 'menu','xcx','modules'));
    }

    public function postEdit()
    {
        $res = $this->wechatMenus->addOrUpdate();
        if (!$res) {
            return $this->error($this->wechatMenus->getError() ? $this->wechatMenus->getError() : '未知错误');
        }
        return $this->success('更新成功', urlAdmin('wechat', 'getMenu'));
    }

    /**
     * 删除一个分类
     */
    public function getRemove()
    {
        $res = $this->wechatMenus->del();
        if (!$res) {
            return $this->error($this->wechatMenus->getError());
        }
        return $this->success($res);
    }

    /*
    * 菜单生成
    */

    public function getMenuSend()
    {
        $res = $this->wechatMenus->sendMenu();
        if (!$res) {
            return $this->error($this->wechatMenus->getError());
        }
        return $this->success($res);
    }



    public function anyChangeStatus()
    {
        $ids = array_unique((array)request('ids'));
        if (empty($ids)) {
            return $this->error('请选择要操作的数据!');
        }
        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $where['id'] = ['in',$ids];
        $data['is_show'] = request('is_show');
        $res = $this->wechatMenus->_update($where,$data);
        if (!$res) {
            return $this->error('操作失败');
        }
        return $this->success('操作成功');
    }

    public function modules()
    {
        return [
            [
                'title' => '商城首页',
                'url' => '/shop/entrance?aid='.is_login(),
            ]
        ];
    }

    public function getQrDown()
    {
        $fileName = $this->wechatSet->qrDownHandle(is_login());
        if(!$fileName){
            return $this->error('微店二维码不存在');
        }

        return response()->download($fileName,time().'_qr.jpg');
    }

}
